package com.golte.dataform.analysis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.golte.dataform.analysis.dao.AdsRptFaBrandRightsDMapper;
import com.golte.dataform.analysis.dao.AdsRptFaChannelUserDMapper;
import com.golte.dataform.analysis.dao.AdsRptFaRightsDMapper;
import com.golte.dataform.analysis.dao.DimFaUserRoleDMapper;
import com.golte.dataform.analysis.service.ReportService;
import com.golte.dataform.analysis.vo.request.ReportReq;
import com.golte.dataform.analysis.vo.response.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    AdsRptFaChannelUserDMapper rptFaChannelUserDMapper;
    @Resource
    AdsRptFaRightsDMapper rightsDMapper;
    @Resource
    AdsRptFaBrandRightsDMapper brandRightsDMapper;
    @Resource
    AdsRptFaChannelUserDMapper channelUserDMapper;
    @Resource
    DimFaUserRoleDMapper dimFaUserRoleDMapper;


    @Override
    public ReportOverviewResp getOverview(ReportReq req) {
        ReportOverviewResp userCount = rptFaChannelUserDMapper.getUserCount(req);
        ReportOverviewResp rightsCount = rptFaChannelUserDMapper.getRightsCount(req);
        Integer userTotal = rptFaChannelUserDMapper.getUserTotal(req);
        rightsCount.setCumRegisterUserCnt(userTotal);
        BeanUtil.copyProperties(userCount, rightsCount, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return rightsCount;
    }

    @Override
    public UserOverviewResp getUserOverview(ReportReq req) {
        List<UserLevelDistribution> userLevelDistribution = rptFaChannelUserDMapper.getUserLevelDistribution(req);
        Map<String, List<UserLevelDistribution>> collect = userLevelDistribution.stream().collect(Collectors.groupingBy(UserLevelDistribution::getUserLevel));

        List<UserLevelDistribution> list = new ArrayList();
        for (Map.Entry<String, List<UserLevelDistribution>> entry : collect.entrySet()) {
            UserLevelDistribution obj = new UserLevelDistribution();
            obj.setRegisterCntRate(0.0);
            obj.setRegisterUserCntSum(0);
            if (entry.getKey().equals("白金卡")) {
                obj.setChilds(entry.getValue());
                list.add(0, obj);
            } else {
                list.add(obj);
            }
            for (UserLevelDistribution item : entry.getValue()) {
                obj.setUserLevel(item.getUserLevel());
                obj.setRegisterUserCntSum(item.getRegisterUserCntSum() + obj.getRegisterUserCntSum());
                obj.setRegisterCntRate(item.getRegisterCntRate() + obj.getRegisterCntRate());
            }
        }

        UserTransform userTransform = rptFaChannelUserDMapper.getUserTransform(req);
        List<UserTrend> userTrend = rptFaChannelUserDMapper.getUserTrend(req);
        List<UserSourceDistribution> userSourceDistribution = rptFaChannelUserDMapper.getUserSourceDistribution(req);
        List<UserChannelDistributionResp> userChannelDistributionResp = rptFaChannelUserDMapper.getUserChannelDistributionResp(req);
        List<Links> links = rptFaChannelUserDMapper.getUserFlow(req);
        Set<String> nodes = new HashSet<>();
        for (Links o : links) {
            nodes.add(o.getSource());
            nodes.add(o.getTarget());
        }
        UserFlow userFlow = new UserFlow();
        userFlow.setNodes(nodes);
        userFlow.setLinks(links);

        UserOverviewResp resp = new UserOverviewResp();
        resp.setUserLevelDistribution(list);
        resp.setUserTransform(userTransform);
        resp.setUserTrend(userTrend);
        resp.setUserSourceDistribution(userSourceDistribution);
        resp.setUserChannelDistributionResp(userChannelDistributionResp);
        resp.setUserFlow(userFlow);
        return resp;
    }

    @Override
    public List<UserChannelDistributionResp> getUserChannelDistribution(ReportReq req) {
        return rptFaChannelUserDMapper.getUserChannelDistributionResp(req);
    }

    @Override
    public RightsOverviewResp getRightsOverview(ReportReq req) {
        RightsChain rightsChain = rightsDMapper.getRightsChain(req);
        List<RightsTrend> rightsTrend = rightsDMapper.getRightsTrend(req);
        RightsOverviewResp resp = new RightsOverviewResp();
        resp.setRightsChain(rightsChain);
        resp.setRightsTrend(rightsTrend);
        return resp;
    }

    @Override
    public RightsTop5Resp getRightsGroupTop5(ReportReq req) {
        List<RightsTypeTop5> rightsTypeTop5 = rightsDMapper.getRightsTypeTop5(req);
        List<RightsTop5> rightsTop5 = rightsDMapper.getRightsTop5(req);
        RightsTop5Resp resp = new RightsTop5Resp();
        rightsTypeTop5.sort(Comparator.comparing(RightsTypeTop5::getSumCnt));
        rightsTop5.sort(Comparator.comparing(RightsTop5::getSumCnt));
        resp.setRightsTypeTop5(rightsTypeTop5);
        resp.setRightsTop5(rightsTop5);
        return resp;
    }


    @Override
    public List<RightsTop5> getRightsTop5(ReportReq req) {
        List<RightsTop5> rightsTop5 = rightsDMapper.getRightsTop5(req);
        rightsTop5.sort(Comparator.comparing(RightsTop5::getSumCnt));
        return rightsTop5;
    }


    @Override
    public IndustryOverviewResp getIndustryOverview(ReportReq req) {
        List<IndustryRights> industryRights = brandRightsDMapper.getIndustryRights(req);
        List<IndustryUser> industryUser = channelUserDMapper.getIndustryUser(req);
        IndustryOverviewResp resp = new IndustryOverviewResp();
        resp.setIndustryRights(industryRights);
        resp.setIndustryUser(industryUser);
        return resp;
    }

    @Override
    public List<Industy> getAllIndustryAndBrand() {
        return dimFaUserRoleDMapper.getAllIndustryAndBrand();
    }

    @Override
    public List<Industy> getIndustryAndBrand(String industryIds, String brandIds) {
        return dimFaUserRoleDMapper.getIndustryAndBrand(industryIds, brandIds);
    }
}
